
// Learn TypeScript:

export class JsNativeBridge {


    protected static callbacks = {}

    static isNative() {
        return this.isCocos() || this.isLaya();
    }

    static isCocos() {
        return window['cc'] != undefined && (window['jsb'] != undefined || window['native'] != undefined)
    }

    static isLaya() {
        return window['conchConfig'] != undefined && window['PlatformClass'] != undefined
    }
    static callStaticMethod(className: string, methodName: string, parameters, methodSignOrFunc?: any) {
        if (JsNativeBridge.isCocos()) {
            JsNativeBridge.cocosCallStaticMethod(className, methodName, parameters, methodSignOrFunc)
        } else {
            JsNativeBridge.layaCallStaticMethod(className, methodName, parameters, methodSignOrFunc)
        }
    }
    static layaCallStaticMethod(className: string, methodName: string, parameters, param) {
        var os = window['conchConfig'].getOS();
        var callback = param.callback;
        var bridge;
        let PlatformClass = window['PlatformClass']
        if (os == "Conch-ios") {
            bridge = PlatformClass.createClass(className);//创建脚步代理
            if (!bridge) {
                console.log(' callStatic Method class name ', className, methodName, parameters)
                return;
            }
            if (callback) {
                bridge.callWithBack(function (value) {
                    var obj = JSON.parse(value)
                    console.log(obj);
                    callback(obj)
                }, methodName + ":", JSON.stringify(parameters));

            } else {
                bridge.call(methodName, parameters)

            }

        }
        else if (os == "Conch-android") {
            //需要完整的类路径，注意与iOS的不同
            bridge = PlatformClass.createClass(className);//创建脚步代理
            if (!bridge) {
                console.log(' callStatic Method class name ', className, methodName, parameters)
                return;
            }
            if (callback) {
                bridge.callWithBack(function (value) {
                    var obj = JSON.parse(value)
                    console.log(obj);
                    callback(obj)
                }, methodName, JSON.stringify(parameters));
            } else {
                bridge.call(methodName, parameters)
            }

        }
    }
    static cocosCallStaticMethod(className: string, methodName: string, parameters, param) {
        let methodSignature = param.methodSignature;
        if (!methodSignature) {
            methodSignature = "(Ljava/lang/String;)V";
        }
        let callback = param.callback;
        if (callback) {
            let c = JsNativeBridge.callbacks[className]
            if (!c) {
                c = {}
                JsNativeBridge.callbacks[className] = c;
            }
            c[methodName] = callback;
        }
        let cc = window['cc']
        let jsb = window['jsb'] || window['native']
        console.log(" JsNativeBridge callStaticMethod ", cc.sys.isNative)
        if (!cc.sys.isNative) {
            return -1;
        }
        console.log(" JsNativeBridge callStaticMethod ", methodName, parameters)
        if (cc.sys.os == cc.sys.OS_IOS) {
            let result = jsb['reflection'].callStaticMethod(className, methodName, parameters);
            if (result) {
                console.log("callStaticMethod ios  ", result);
            }
        } else {
            console.log(" JsNativeBridge callStaticMethod adroid ")
            let result = jsb['reflection'].callStaticMethod(className, methodName, methodSignature, parameters)
            console.log("callStaticMethod result  ", result);
            if (result) {

                return result;
            }
        }
    }
    static javaCallback(data) {
        if (data) {
            data = JSON.parse(data)
            if (data) {
                let className = data.className;
                let methodName = data.methodName;
                let c = JsNativeBridge.callbacks[className]
                if (c) {
                    let callback = c[methodName]
                    if (callback) {
                        callback(data.result)
                        c[methodName] = null;
                    }
                }
            }

        }
    }
    static bannerResize(data) {

    }
    static rewardAdClose(data) {

    }

}
